
public class CheckBoxes{
  
  String boxTitle; // title of section holding checkboxes
  float boxAreaX1, boxAreaY1;
  float boxAreaX2, boxAreaY2;
  
  int numOfBoxes;
  int[] boxColors;
  String[] boxLabels;
  float[] boxX1;
  float[] boxY1;
  float[] boxX2;
  float[] boxY2;
  
  float theSize;
  
  boolean isHorizontal;
  
  boolean[] isChecked;
  
  CheckBoxes(int numOfBoxes, String boxTitle, String[] boxLabels, float boxAreaX1, float boxAreaY1, float boxAreaX2, float boxAreaY2, int[] boxColors){
    this.numOfBoxes = numOfBoxes;
    this.boxTitle = boxTitle;
    this.boxLabels = boxLabels;
    this.boxAreaX1 = boxAreaX1;
    this.boxAreaY1 = boxAreaY1;
    this.boxAreaX2 = boxAreaX2;
    this.boxAreaY2 = boxAreaY2;
    this.boxColors = boxColors;
    
    //now determine positions of boxes
    
    boxX1 = new float[ numOfBoxes ];
    boxY1 = new float[ numOfBoxes ];
    boxX2 = new float[ numOfBoxes ];
    boxY2 = new float[ numOfBoxes ];
    
    isChecked = new boolean[numOfBoxes];
    
    if( boxAreaX2-boxAreaX1 < boxAreaY2 - boxAreaY1 ){
      theSize = (boxAreaY2 - boxAreaY1)/(numOfBoxes * 2 + 1);
      isHorizontal = false;
      
      //locate check boxes
      float prevY = boxAreaY1;
      for(int i = 0; i < numOfBoxes; i++){
        boxX1[i] = boxAreaX1 + theSize;
        boxX2[i] = boxX1[i] + theSize;
        boxY1[i] = prevY + theSize;
        boxY2[i] = boxY1[i] + theSize;
        prevY = boxY2[i];
        isChecked[i] = true;
      }
    } else{ //horizontal
      theSize = (boxAreaX2 - boxAreaX1)/(numOfBoxes * 2 + 1);
      isHorizontal = true;
       //locate check boxes
      float prevX = boxAreaX1;
      for(int i = 0; i < numOfBoxes; i++){
        boxX1[i] = prevX + theSize;
        boxX2[i] = boxX1[i] + theSize;
        boxY1[i] = boxAreaY1 + theSize*2;
        boxY2[i] = boxY1[i] + theSize;
        prevX = boxX2[i];
        isChecked[i] = true;
      }
    }//end else
    
  }//end constructor
  
  void drawCheckBoxes(){
    //first draw rectangle to hold boxes
    rectMode(CORNERS);
    noStroke();
    fill( 100, 100, 100, 125);
    rect(boxAreaX1, boxAreaY1, boxAreaX2, boxAreaY2);
    
    stroke(0);
    for(int i = 0; i < numOfBoxes; i++){
      fill(boxColors[i]);
      if( mouseX > boxX1[i] && mouseX < boxX2[i] && mouseY > boxY1[i] && mouseY < boxY2[i] ){ //mouse is over
        strokeWeight(2);
      }else{
        strokeWeight(1);
      }
      rect(boxX1[i], boxY1[i], boxX2[i], boxY2[i] );
      if( isChecked[i] ){
        noFill();
        line(boxX1[i], boxY1[i], boxX2[i], boxY2[i]);
        line(boxX2[i], boxY1[i], boxX1[i], boxY2[i]);
      } else{
        fill(100, 100, 100, 125);
        rect(boxX1[i], boxY1[i], boxX2[i], boxY2[i] );
      }
      fill(255);
      textSize(3*theSize/4);
      textAlign(CENTER);
      //rotate(PI);
      text(boxLabels[i], (boxX1[i]+boxX2[i])/2, boxY2[i]+theSize);
    }
    //draw title rect and title
    fill(100, 100, 100, 125);
    noStroke();
    textSize(theSize);
    rect(boxAreaX1, boxAreaY1-theSize, boxAreaX1 + textWidth(boxTitle) + theSize, boxAreaY1);
    strokeWeight(1);
    fill(255);
    textAlign(LEFT);
    text(boxTitle, boxAreaX1+theSize/2, boxAreaY1);
   
    rectMode(CORNER);//reset for map class
  }
  
  boolean[] isChecked( float mX, float mY){
    //println("mouse click");
    for(int i = 0; i < numOfBoxes; i++){
      if( mX > boxX1[i] && mX < boxX2[i] && mY > boxY1[i] && mY < boxY2[i] ){
        //println("i = " + i);
        isChecked[i] = ! isChecked[i]; //reverse value
      }
    }
    return isChecked;
  }//end method isChecked
  
  
  
}//end class

